import { Controller, Get, Post, Put, Delete, Body, Param, Query, DefaultValuePipe, ParseIntPipe } from '@nestjs/common';
import { HomeService, PaginatedHomes } from './home.service';
import { Home, BannerType } from './home.entity';


@Controller('home')
export class HomeController {
  constructor(private readonly homeService: HomeService) { }

  // 创建轮播图
  @Post('banners/create')
  async createHomeBanner(@Body() createData: {
    title: string;
    description?: string;
    images?: any;  // 图片
    image_url?: string;  // 图片URL
    link_url?: string;
    sort_order?: number;
    is_visible?: boolean; // 是否显示
  }): Promise<Home> {
    // return this.homeService.create(createData);
    // 强制指定type为HOME，避免与design混淆
    return this.homeService.create({ ...createData, type: BannerType.HOME } as any);
  }

  // 修改
  @Post('/banners/update')
  async updateHomeBanner(
    @Body()
    updates: {
      id: number;
      title: string;
      description?: string;
      images?: any;  // 图片
      image_url?: string;  // 图片URL
      link_url?: string;
      sort_order?: number;
      is_visible?: boolean; // 是否显示
    },
  ) {
    return this.homeService.update(
      updates.id, updates, BannerType.HOME
    );
  }

  // 获取所有轮播图（后台管理用）
  @Get('banners')
  async findAllHomeBanners(): Promise<Home[]> {
    return this.homeService.findAllByType(BannerType.HOME);
  }

  // 获取所有轮播图（分页，后台管理用）
  @Get('banners/home/all')
  async findHomeBannersPaginated(
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number = 10,
  ): Promise<PaginatedHomes> {
    // 限制每页最大数量
    limit = Math.min(limit, 100);
    return this.homeService.findAllByTypePaginated(BannerType.HOME, page, limit);
  }

  // 删除
  @Delete('/banners/delete/:id')
  async deleteHomeBanner(@Param('id') id: number): Promise<void> {
    return this.homeService.delete(id, BannerType.HOME);
  }

  // ---------------------- 设计页面轮播图接口 ----------------------
  /**
   * 创建设计页面轮播图
   * 自动指定type为DESIGN
   */
  @Post('design-banners/create')
  async createDesignBanner(
    @Body() createData: {
      title: string;
      description?: string;
      images?: any;
      image_url?: string;
      link_url?: string;
      sort_order?: number;
      is_visible?: boolean;
      type?: BannerType; // 虽然允许传入，但会被覆盖
    }
  ): Promise<Home> {
    // 强制指定type为DESIGN
    return this.homeService.create({ ...createData, type: BannerType.DESIGN } as any);
  }

  /**
   * 更新设计页面轮播图
   */
  @Post('design-banners/update')
  async updateDesignBanner(
    @Body()
    updates: {
      id: number;
      title: string;
      images?: any;
      image_url?: string;
      link_url?: string;
      sort_order?: number;
      is_visible?: boolean;
    }
  ): Promise<Home> {
    // 仅允许更新type为DESIGN的轮播图
    return this.homeService.update(updates.id, updates, BannerType.DESIGN);
  }

  /**
   * 获取设计页面所有轮播图（管理用）
   */
  @Get('design-banners')
  async findAllDesignBanners(): Promise<Home[]> {
    return this.homeService.findAllByType(BannerType.DESIGN);
  }

  /**
   * 设计页面轮播图分页查询（管理用）
   */
  @Get('design-banners/paginated')
  async findDesignBannersPaginated(
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number = 10,
  ): Promise<PaginatedHomes> {
    limit = Math.min(limit, 100);
    return this.homeService.findAllByTypePaginated(BannerType.DESIGN, page, limit);
  }

  /**
   * 删除设计页面轮播图
   */
  @Delete('design-banners/delete/:id')
  async deleteDesignBanner(@Param('id') id: number): Promise<void> {
    return this.homeService.delete(id, BannerType.DESIGN);
  }

  /**
   * 设计页面前端展示用（只返回可见的）
   */
  @Get('design-banners/active')
  async getActiveDesignBanners(): Promise<Home[]> {
    return this.homeService.findActiveByType(BannerType.DESIGN);
  }
}
